#include <stdio.h>
#include <set>
using namespace std;
#define N 1010
char m[N][2*N];
int s[N][2*N], d1[N][2*N], d2[N][2*N];
set <int> w;
int main()
{
	int i, j, r, c, k, n, q, A;
	long long t;
	for(scanf("%d%d", &n, &q), i=0; i<n; scanf("%s", m[i]+N-i), i++);
	for(i=n-1; i>=0; i--)
		for(j=N-i; j<=N+i; j++)
		{
			s[i][j]=s[i+1][j]+s[i][j-1]-s[i+1][j-1]+m[i][j]-'0';
			d1[i][j]=d1[i+1][j-1]+s[i+1][j]-s[i+1][j-1]+m[i][j]-'0';
			d2[i][j]=d2[i+1][j+1]+s[i+1][j]-s[i+1][j-1]+m[i][j]-'0';
		}
	for(t=0, A=1; q--; A=((long long)A*1234567+7654321)%1000000007)
	{
		r=A%n+1;
		c=A%(2*r-1)+1;
		k=A%(n-r+1)+1;
		j=c-r+N; i=r-1;
		t+=d1[i][j]+d2[i+1][j+1]-d1[i+k][j-k]-d2[i+k][j+k]-s[i+k][j+k-1]+s[i+k][j-k];
	}
	printf("%lld\n", t);
	return 0;
}